const IDCard=require('../../utils/idcard.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    model:'member',
    radioCheckVal: 4,
    isSex: "",
    isSend:1,
    birthday: "",
    information: [],
    userSex: '',
    userCoat: '',
    userBlood: '',
    modalHidden: true,
    send: false,
    alreadySend: false,
    second: 60,
    disabled: true,
    buttonType: 'default',
    phoneNum: '',
    code: '',
    showModal: false,
    showCode: false,
    otherInfo: '',
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    coatArray: ["请选择","XS", "S", "M", "L", "XL", "XXL", "XXXL"],
    coat:'请选择',
    bloodArray: ["请选择","O","A" ,"B","AB"],
    blood:'请选择',
    region: ['山东', '临沂市', '兰山'],
    unitIndex:0

  },
  //单选按钮发生变化
  radioCheckedChange: function (e) {

    this.setData({

      radioCheckVal: e.detail.value,
      isSex: e.detail.value

    })

  },
  bindTeamChange:function(e){
    var unitIndex=e.detail.value;
    var unit=this.data.team[unitIndex].name;
    this.setData({
      unitIndex:unitIndex,
      unit:unit
    })
  },
  yinsi:function(){
    wx.navigateTo({
      url: 'yinsi',
    })
  },
  xieyi:function(){
    wx.navigateTo({
      url: 'xieyi',
    })
  }, 
  bindCoatChange:function(e) {
   // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
     coat: this.data.coatArray[e.detail.value]
    })
  },
  bindBloodChange:function(e) {
    // console.log('picker发送选择改变，携带值为', e.detail.value)
     this.setData({
      blood: this.data.bloodArray[e.detail.value]
     })
   },
  inputIdCard2: function (e) {
    let idcard = e.detail.value;
    //console.log(idcard);
    if (idcard) {
      if (idcard.length == 18) {
        var retrun = IDCard.IdCard(idcard, 4);
        if (retrun == "error") {
          wx.showToast({
            title: '身份证号码不正确',
            image: '../../icon/fail.png'
          })
          this.setData({
            idcardfocus: true
          })
        } else {
          var retrun = IDCard.IdCard(idcard, 2); var sexvalue = 1;
          if (retrun == "男") { sexvalue = 0; }
          this.setData({
            radioCheckVal: sexvalue,
            isSex: sexvalue
          })
          var retrun = IDCard.IdCard(idcard, 1);
          this.setData({
            birthday: retrun
          })
          //console.log(retrun);
        }
      }
    }
  },
  inputIdCard: function (e) {
    let idcard = e.detail.value;
    //console.log(idcard);
    if (idcard) {
      if (idcard.length == 18) {
        var retrun = IDCard.IdCard(idcard, 4);
        if (retrun == "error") {
          wx.showToast({
            title: '身份证号码不正确',
            image: '../../icon/fail.png'
          })
          this.setData({
            idcardfocus: true
          })
        } else {
          var retrun = IDCard.IdCard(idcard, 2); var sexvalue = 1;
          if (retrun == "男") { sexvalue = 0; }
          this.setData({
            radioCheckVal: sexvalue,
            isSex: sexvalue
          })
          var retrun = IDCard.IdCard(idcard, 1);
          this.setData({
            birthday: retrun
          })
          //console.log(retrun);
        }
      } else {
        wx.showToast({
          title: '身份证号码长度不对',
          image: '../../icon/fail.png'
        })
        this.setData({
          idcardfocus: true
        })
      }
    }
  },
  
  inputPhoneNum: function (e) {
    let phoneNum = e.detail.value
    if (phoneNum.length === 11) {
      let checkedNum = this.checkPhoneNum(phoneNum)
      if (checkedNum) {
        app.globalData.phoneNum = phoneNum;
        this.setData({
          phoneNum: phoneNum
        })
        console.log('phoneNum' + this.data.phoneNum)
      //this.showSendMsg()
        //this.activeButton()
      }
    } else {
      this.setData({
        phoneNum: ''
      })
      //this.hideSendMsg()
    }
  },

  checkPhoneNum: function (phoneNum) {
    let str = /^1[3,4,5,7,8,99]\d{9}$/
    if (str.test(phoneNum)) {
      return true
    } else {
      wx.showToast({
        title: '手机号不正确',
        image: '../../icon/fail.png'
      })
      return false
    }
  },

  
  addOtherInfo: function (e) {
    this.setData({
      otherInfo: e.detail.value
    })
    this.activeButton()
    console.log('otherInfo: ' + this.data.otherInfo)
  },

  
  
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  
  checkUserName: function (e) {
    var username = e.detail.value;
    username = username.trim();
    //console.log(username)
    if (username == "") {
      //console.log(username)
      wx.showToast({
        title: '姓名不能为空',
        image: '../../icon/fail.png'
      })
      this.setData({
        namefocus: true
      })
    }
  },
  checkLinkName: function (e) {
    var linkname = e.detail.value;
    linkname = linkname.trim();
    //console.log(username)
    if (linkname == "") {
      //console.log(username)
      wx.showToast({
        title: '紧急联系人不能为空',
        image: '../../icon/fail.png'
      })
      this.setData({
        linknamefocus: true
      })
    }
  },
  checkLinkPhone: function (e) {
    var linkphone = e.detail.value;
    linkphone = linkphone.trim();
    //console.log(username)
    if (linkphone == "") {
      //console.log(username)
      wx.showToast({
        title: '紧急联系电话不能为空',
        image: '../../icon/fail.png'
      })
      this.setData({
        linkphonefocus: true
      })
    }
  },
  //表单提交
  formSubmit(e) {
    //console.log(this.data.isSex);
    var userSex = this.data.isSex == 0?'男' : '女';
    var information = e.detail.value;
    
    if (information.name == "") {
      wx.showToast({
        title: '用户名不能为空',
        image: '../../icon/fail.png'
      })
    } else {
      if (information.idNumber == "") {
        wx.showToast({
          title: '身份证号码不能为空',
          image: '../../icon/fail.png'
        })
      } else {
        if (information.phone == "") {
          wx.showToast({
            title: '电话号码不能为空',
            image: '../../icon/fail.png'
          })
        } else {
          if (information.linkname == "") {
            wx.showToast({
              title: '紧急联系人不能为空',
              image: '../../icon/fail.png'
            })
          } else {
            if (information.linkphone == "") {
              wx.showToast({
                title: '紧急联系电话号码不能为空',
                image: '../../icon/fail.png'
              })
            } else {

              
                //console.log(userSex);
                this.setData({
                  information: e.detail.value,
                  userSex:userSex,
                  userBlood:this.data.blood,
                  userCoat:this.data.coat,
                  userIDAddress:this.data.region[0]+'-'+this.data.region[1]+'-'+this.data.region[2],
                  showModal: true
                });
              
            }
          }
        }
      }
    }
  },
  modal_click_Hidden: function () {
    this.setData({
      showModal: false,
    })
  },
  // 确定
  confirmSubmit(e) {
    //if(){}
    //console.log(e.detail.value)
    var information = e.detail.value;
    var open=wx.getStorageSync('open');
    var data=this.data;
    var that = this;
    var unionid=open.unionid;
    wx.request({
      url: data.domain+data.model+'/member/addMember',	//本地设置不校验合法域名
      data: {unionid : unionid,name : information.myname,card: information.myidcard, sex: information.mysex, phone: information.myphone, coat: information.mycoat, blood: information.myblood, linkname: information.mylinkname, linkphone: information.mylinkphone, address: information.detailaddress,unit:information.unit,unitIndex:that.data.unitIndex},
      success: function (res) {
        //console.log(res)
        that.setData({
          showModal: false,
        })
       // wx.navigateBack();
       if(res.data.state=='already'){
        wx.showModal({
          title: '提示',
          content: '该身份证已经申请，请绑定会员',
          success (res) {
            if (res.confirm) {
              wx.navigateBack()
            }else{
              wx.navigateBack()
            }
          }
        })
       }
       if(res.data.state=='success'){
         app.globalData.open.member_id=res.data.member_id;
         app.globalData.open.bind=1;
        wx.showModal({
          title: '提示',
          content: '申请成功，等待管理员审核',
          success (res) {
            if (res.confirm) {
              wx.navigateBack()
            }else{
              wx.navigateBack()
            }
          }
        })
       }
      }
    })

  },
  bindRegionChange: function (e) {
   // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bind:function(){
    wx.navigateTo({
      url: 'bind',
    })
  },
  getTeam:function(){
    let that=this;
   console.log('111');
    wx.request({
      url:app.globalData.domain+'member/unit/getUnit',
      success:res=>{
        console.log(res);
        that.setData({
          team:res.data
        })
      }
    })
  },
  goBack:function(){
    wx.navigateBack();
  },
  onLoad: function (options) {
    this.getTeam();
    var open =wx.getStorageSync('open');
    if(open.member_id>0){
        app.showModals("您已经是会员","/pages/default/index","index")
    }
    this.setData({
      domain:app.globalData.domain,
        width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop
    })
    // console.log('初始化完成');


  }
})